//
//  UICollectionView+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//
#import "UICollectionView+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation UICollectionView (THHook)

+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ExchangeImplementationsInTwoClasses([UICollectionView class],@selector(scrollToRowAtIndexPath:atScrollPosition:animated:),
                                    [UICollectionView class], @selector(qmui_scrollToItemAtIndexPath:atScrollPosition:animated:));
    });
}
- (void)qmui_scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated{
     BOOL isIndexPathLegal = YES;
    NSInteger numberOfSections = [self numberOfSections];
    if (indexPath.section >= numberOfSections) {
        isIndexPathLegal = NO;
    } else {
        NSInteger items = [self numberOfItemsInSection:indexPath.section];
        if (indexPath.item >= items) {
            isIndexPathLegal = NO;
        }
    }
    if (!isIndexPathLegal) {
        NSLog(@"UICollectionView (QMUI)%@ - target indexPath : %@ ，不合法的indexPath。\n%@", self, indexPath, [NSThread callStackSymbols]);
              NSString *errorDes = [NSString stringWithFormat:@"UICollectionView (QMUI)%@ - target indexPath : %@ ，不合法的indexPath。\n%@",self, indexPath, [NSThread callStackSymbols]];
        NSError *error = [NSError errorWithDomain:@"huanque" code:10000 userInfo:@{NSLocalizedDescriptionKey:errorDes}];
        [[THCrashManager shareInstance] reportError:error];
        return;
    }
    [self qmui_scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}

@end
